﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DIProject.Stock
{
    public partial class ShiftOutByIncome : Form
    {
        public ShiftOutByIncome()
        {
            InitializeComponent();
        }

        private void ShiftOutByIncome_Load(object sender, EventArgs e)
        {
            try
            {
                departmentsTableAdapter.Fill(dictionary.Departments);
                shiftOutByIncomeTableAdapter.Fill(stock.ShiftOutByIncome);
                RedGrid();
            }
            catch (SqlException eq)
            {
                MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void RedGrid()
        {
            foreach (DataGridViewRow r in gridView.Rows)
            {
                DataRowView drv = r.DataBoundItem as DataRowView;
                if (drv == null)
                    continue;
                DataSets.Stock.ShiftOutByIncomeRow sr = drv.Row as DataSets.Stock.ShiftOutByIncomeRow;
                if (sr == null)
                    continue;
                ///если документ выгружался, красим в желтый
                if (!sr.IsRIDNull())
                {
                    r.DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
        }
        private void ShiftOutByIncome_Resize(object sender, EventArgs e)
        {
            comboBoxDepartment.Size = new Size(ClientSize.Width - labelDepartment.Left - comboBoxDepartment.Left, comboBoxDepartment.Height);
        }

        private void gridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.Handled = true;
        }

        private void gridView_Sorted(object sender, EventArgs e)
        {
            this.RedGrid();
        }
        bool isFirst = false;
        bool isOne = true;
        private void gridView_SelectionChanged(object sender, EventArgs e)
        {
            if (isOne)
            {
                isOne = false;
                return;
            }
            if (isFirst)
            {
                isFirst = false;
                return;
            }
            else
            {
                isFirst = true;
            }
            DataGridViewRow r = gridView.CurrentRow;
            if (r == null)
                return;
            DataRowView drv = r.DataBoundItem as DataRowView;
            if (drv == null)
                return;
            DataSets.Stock.ShiftOutByIncomeRow sr = drv.Row as DataSets.Stock.ShiftOutByIncomeRow;
            if (sr == null)
                return;
            if (!sr.IsRIDNull())
            {
                MessageBox.Show("Данный документ уже выгружен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                r.Selected = false;
                isFirst = false;
                return;
            }
            isFirst = false;
        }
    }
}